<template>
  <a-layout id="components-layout-demo-top" class="layout">
    <a-layout-header>
      <div class="left-container">
        <div class="logo" />
        <a-menu
          theme="dark"
          mode="horizontal"
          v-model:selectedKeys="selectedKeys"
          :style="{ lineHeight: '64px' }"
        >
          <a-menu-item key="1">
            广场
          </a-menu-item>
        </a-menu>
      </div>
      <div class="right-container">
        <a-dropdown>
          <div>
            <a-avatar size="small" :src="currentUser.avatar"> </a-avatar>
            <a class="ant-dropdown-link" @click="e => e.preventDefault()"
              >{{ currentUser.realName }} <DownOutlined />
            </a>
          </div>
          <template v-slot:overlay>
            <a-menu @click="handleMenuClick">
              <a-menu-item key="1">
                个人中心
              </a-menu-item>
              <a-menu-item key="2">
                个人设置
              </a-menu-item>
              <a-menu-item key="3">
                退出登录
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </div>
    </a-layout-header>
    <a-layout-content style="padding: 24px">
      <router-view />
    </a-layout-content>
    <a-layout-footer style="text-align: center">
      Ant Design ©2020 Created by Ant UED
    </a-layout-footer>
  </a-layout>
</template>
<script lang="ts">
import { loginRoutePath } from "@/const/path";
import { DownOutlined, ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { useStore } from "@/store";
import { computed, createVNode, defineComponent, ref } from "vue";
import { Modal } from "ant-design-vue";
import router from "@/router";
import { ActionTypes } from "@/store/modules/user/action-types";
export default defineComponent({
  components: {
    DownOutlined,
  },
  setup() {
    const store = useStore();
    const selectedKeys = ref(["1"]);
    const currentUser = computed(() => {
      return {
        realName: store.getters.userInfo?.realName ?? "",
        avatar: store.getters.userInfo?.avatar ?? "",
      };
    });
    const handleCenter = () => {
      router.push({
        path: "/center/index",
      });
    };
    const handleUserLogout = () => {
      Modal.confirm({
        title: "提示",
        icon: createVNode(ExclamationCircleOutlined),
        content: "真的要注销登录吗 ?",
        okText: "确认",
        cancelText: "取消",
        onOk: async () => {
          try {
            await store.dispatch(ActionTypes.LOGOUT_USER);
          } catch (error) {
            console.log(error);
          } finally {
            router.push({
              path: loginRoutePath,
            });
          }
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
          // }).catch(() => console.log("Oops errors!"));
        },
        onCancel() {
          console.log("取消退出");
        },
      });
    };
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const handleMenuClick = (e: any) => {
      switch (e.key) {
        case "1":
          handleCenter();
          break;
        case "3":
          handleUserLogout();
          break;

        default:
          console.log(e.key);
          break;
      }
    };
    return {
      currentUser,
      selectedKeys,
      handleMenuClick,
    };
  },
});
</script>
<style lang="scss" scoped>
#components-layout-demo-top {
  .ant-layout-header {
    display: flex;
    justify-content: space-between;
    .left-container {
      display: flex;
      justify-content: flex-start;
      flex: 1;
      .logo {
        width: 120px;
        height: 31px;
        background: rgba(255, 255, 255, 0.2);
        margin: 16px 24px 16px 0;
      }
    }
  }
}
</style>